#include "PartManager.h"
#include "User.h"
#include "AdminMenu.h"
#include "StoreIOManager.h"
#include "StoreCheck.h"

#include "Login.h"
#include "ui_Login.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    setWindowIcon(QIcon(":/image/CMSISIcom.svg"));

    setWindowFlags(Qt::FramelessWindowHint | windowFlags());//设置无边框
    setAttribute(Qt::WA_TranslucentBackground);//设置背景为透明的

    m_databaseManager = DatabaseManager::getInstance();
    try{
        m_databaseManager->initDatabase();
    }catch(const std::exception& e)
    {
        QMessageBox::critical(this,"数据库初始化提示",e.what());
    }

    connect(ui->btn_login,&QPushButton::clicked,this,&Widget::m_on_btn_login_clicked);
    connect(ui->btn_exit,&QPushButton::clicked,this,&Widget::close);

    ui->le_name->setText(defaultUserName);
    ui->le_password->setText(defaultUserPassword);
}

Widget::~Widget()
{
    delete ui;

    qDebug() << "登录界面销毁";
}

void Widget::m_on_btn_login_clicked()
{
    User tmpLoginUserInfo;
    try{
        tmpLoginUserInfo = m_databaseManager->login_getUserInfo(ui->le_name->text(),ui->le_password->text());
    }catch(const std::exception& e)
    {
        QMessageBox::information(this,"登录信息提示",e.what());
        ui->le_name->clear();
        ui->le_password->clear();
        return;
    }

    switch(tmpLoginUserInfo.getRole())
    {
        case Role::ADMIN:
        {
            AdminMenu* adminMenu = new AdminMenu(tmpLoginUserInfo,nullptr);
            adminMenu->setWindowFlags(adminMenu->windowFlags() | Qt::WindowStaysOnTopHint| Qt::Window);
            adminMenu->show();
            this->close();
            break;
        }
        case Role::USER_INFO:
        {
            PartManager* partMenu = new PartManager(tmpLoginUserInfo,nullptr);
            partMenu->show();
            this->close();
            break;
        }
        case Role::USER_STORE_IO:
        {
            StoreIOManager* storeIOMenu = new StoreIOManager(tmpLoginUserInfo,nullptr);
            storeIOMenu->show();
            this->close();
            break;
        }
        case Role::USER_STORE_CHECK:
        {

            StoreCheck* storeIOMenu = new StoreCheck(tmpLoginUserInfo,nullptr);
            storeIOMenu->show();
            this->close();
            break;
        }
        default:
            QMessageBox::information(this,"人员信息提示","当前用户的角色信息异常");
            break;

    }


}
